package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.NursingLevelMapper;
import com.itheima.pojo.NursingLevelPage;
import com.itheima.pojo.NursingPage;

import com.itheima.pojo.NursingLevel;
import com.itheima.service.NursingLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 分页查询数据
     * @param nursingPage
     * @return
     */
    @Override
    public NursingLevelPage page(NursingPage nursingPage) {
        //利用PageHelper可以让执行动态SQL语句的时候自动计算出索引值和每页行数
        PageHelper.startPage(nursingPage.getPage(),nursingPage.getPageSize());
        //将查出来的数据放到list集合中
        List<NursingLevel> list =  nursingLevelMapper.page(nursingPage.getName());
        //利用Page的强转将数据后续可以通过.getTotal() 和.getResult()获取总行数和相关list集合数据
        Page<NursingLevel> pageList = (Page<NursingLevel>) list;
        return new NursingLevelPage(pageList.getTotal(),pageList.getResult());
    }

    /**
     * 新增数据
     */
    @Override
    public void add(NursingLevel np) {
        //将创建时间和更新时间进行更新
        np.setCreateTime(LocalDateTime.now());
        np.setUpdateTime(LocalDateTime.now());
        //新增成功返回true;失败返回false

        nursingLevelMapper.add(np);

    }

    /**
     * 删除数据
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        nursingLevelMapper.delete(ids);
    }

    /**
     * 修改数据第一步回显
     * @param id
     * @return
     */
    @Override
    public NursingLevel getById(Integer id) {
        return nursingLevelMapper.getById(id);
    }

    /**
     * 修改数据第二步更新操作
     * @param np
     */
    @Override
    public void update(NursingLevel np) {
        np.setUpdateTime(LocalDateTime.now());
        nursingLevelMapper.update(np);
    }
}
